package com.gitlab.lucky.weixin.service;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.gitlab.lucky.common.dao.mapper.WeixinGzhRechargeMapper;
import com.gitlab.lucky.common.dao.model.WeixinGzhRecharge;
import com.gitlab.lucky.common.dao.model.WeixinGzhUser;
import com.gitlab.lucky.common.enums.Constant;
import com.gitlab.lucky.common.service.ConfigDataService;
import com.gitlab.lucky.common.service.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @program: lucky
 * @author: Mr.Han
 * @create: 2019-04-17 14:58
 **/
@Service
public class WeixinGzhRechargeService extends ServiceImpl<WeixinGzhRechargeMapper,WeixinGzhRecharge> {
    @Autowired
    private ConfigDataService configDataService;
    @Autowired
    private WeixinGzhRechargeMapper weixinGzhRechargeMapper;
    @Autowired
    private WeixinGzhUserService weixinGzhUserService;
    /**
     * 查询我的佣金
     * @param openId
     */
    public HashMap<Integer, Double> selectCommission(String openId,Integer platform,Integer state) {
        Example example = new Example(WeixinGzhRecharge.class);
        example.createCriteria()
                .andEqualTo("parentOpenId",openId)
                .andEqualTo("platform",platform)
                .andNotEqualTo("state",state);
        List<WeixinGzhRecharge> commission = weixinGzhRechargeMapper.selectByExample(example);
        Double success = 0.0;
        Double withdrawal = 0.0;
        Double noWithdrawal = 0.0;
        for (WeixinGzhRecharge weixinGzhRecharge : commission) {
            if(weixinGzhRecharge.getState().equals(Constant.WITHDRAWAL_SUCCESS)){
                success+=weixinGzhRecharge.getMoney();
            }
            if(weixinGzhRecharge.getState().equals(Constant.WITHDRAWAL)){
                withdrawal+=weixinGzhRecharge.getMoney();
            }
            if(weixinGzhRecharge.getState().equals(Constant.NO_WITHDRAWAL)){
                noWithdrawal+=weixinGzhRecharge.getMoney();
            }
        }
        HashMap<Integer, Double> map = new HashMap<>();
        map.put(Constant.WITHDRAWAL_SUCCESS,success);
        map.put(Constant.WITHDRAWAL,withdrawal);
        map.put(Constant.NO_WITHDRAWAL,noWithdrawal);
        return map;
    }

    /**
     * 佣金记录
     * @param pageNum
     * @param pageSize
     * @param openId
     * @param platform
     * @return
     */
    public PageInfo<WeixinGzhRecharge> selectCommissionInfo(Integer pageNum, Integer pageSize, String openId, Integer platform) {
        JSONObject config = configDataService.getConfig(platform);
        Example example = new Example(WeixinGzhRecharge.class);
        example.createCriteria()
                .andEqualTo("parentOpenId",openId)
                .andEqualTo("platform",platform);
        PageInfo<WeixinGzhRecharge> weixinGzhRechargePageInfo = this.selectPage(pageNum, pageSize, example);
        for (WeixinGzhRecharge weixinGzhRecharge : weixinGzhRechargePageInfo.getList()) {
            WeixinGzhUser weixinGzhUser = weixinGzhUserService.getWxUserInfoByOpenId(weixinGzhRecharge.getOpenId(), platform);
            weixinGzhRecharge.setImageUrl(weixinGzhUser.getHeadimgurl());
            weixinGzhRecharge.setNickName(weixinGzhUser.getNickname());
            weixinGzhRecharge.setMoneyStr(config.getDouble(Constant.COMMISSION)*weixinGzhRecharge.getMoney()+"");
        }
        return weixinGzhRechargePageInfo;
    }
}
